Repository navigation
Downcast nanosecond timestamps and times before partitioning - #4479
rahulsmahadev wants to merge 4 commits into
Conversation
Partition values were computed on timestamp[ns] input before the writer downcast it, so identity partitions duplicated or lost rows, hour partitions failed, and negative year/month/day partitions disagreed with the stored values. Cast each nanosecond partition source to timestamp[us] before its transform when downcast-ns-timestamp-to-us-on-write is set. Co-authored-by: Isaac <no-reply@databricks.com>
|
@ebyhr, this fixes hour and identity partitions on timestamp[ns] when downcast-ns-timestamp-to-us-on-write is on, by computing partition values on the microseconds that get written. Could you take a look when you have a chance? Thanks! |
Cover identity, hour and UTC day partitions in one loop over the same values, tie each partition to its rows in the native ns test, and move the tests after the related _to_requested_schema pair. Co-authored-by: Isaac <no-reply@databricks.com>
|
I tested seven synthetic cases across Iceberg v1/v2 tables, covering identity, hour, and UTC-day partitions. All passed with this fix, with correct stored rows, partition values, and filtered scans. I also checked the existing-file caveat mentioned in the PR. I wrote two separate rows with the same input timestamp (
A full scan followed by filtering returned both rows, but filtering directly in Handling affected legacy files separately makes sense and keeps this PR focused. |
Co-authored-by: Isaac <no-reply@databricks.com>
apache#4471 made the downcast option cover time64[ns] as well, with the same ordering problem: an identity partition on a time column grouped rows by nanoseconds, so 1000 and 1500 ns (both 1 us) wrote one row twice and dropped the other, and an unaligned value alone in its microsecond raised ZeroDivisionError. Cast ns times to microseconds before the transform, as for timestamps. Co-authored-by: Isaac <no-reply@databricks.com>
Rationale for this change
Bug: with
downcast-ns-timestamp-to-us-on-writeon (non-default; set here withPYICEBERG_DOWNCAST_NS_TIMESTAMP_TO_US_ON_WRITE=true), an hour partition on atimestamp[ns]column fails. The partition values are computed on the nanosecond input, and the writer only downcasts afterwards. The hour transform has no nanosecond kernel:For an identity partition on the same column, main writes the aligned row twice and drops the unaligned one (1000 and 1500 ns give IDs
[0, 0]), and raisesZeroDivisionErrorwhen no aligned row shares an unaligned value's microsecond. For year, month and day partitions, a -1 ns row is stored as 0 us but filed under partition -1, so a filter likets >= '1970-01-01T00:00:00'skips it.Since #4471 the option also downcasts
time64[ns], and an identity partition on atimecolumn has the same problem: 1000 and 1500 ns give IDs[0, 0], and an unaligned value alone in its microsecond raisesZeroDivisionError.Fix: when the option is on, cast each nanosecond partition source (timestamp or time) to microseconds with the writer's safe=False cast before applying its transform. The writer still downcasts the data, so partition values match stored values. Refs #1619, the same conversion-order problem for the day transform, closed as stale.
Java's v1/v2 partition transforms receive microseconds: Spark supplies them directly, and the generic Kafka writer uses InternalRecordWrapper to convert timestamps and times first. This PR applies PyIceberg's existing downcast before partitioning.
Are these changes tested?
Yes. Three tests in
tests/io/test_pyarrow.py, next to the existing downcast tests.test_append_partitioned_ns_timestamp_downcastappends 1000, 1500, -1, 3600000000000 ns and a null to identity, hour and UTC day partitions, and checks the stored microseconds and the rows per partition. Each case fails on main: identity withZeroDivisionError, hour with theFeatureUnsupportederror above, and day with the -1 ns row in partition -1.test_append_partitioned_ns_time_downcastdoes the same for an identity partition on atimecolumn (1000, 1500 and 3600000001500 ns and a null); it fails on main withZeroDivisionError.test_determine_partitions_native_ns_timestamp_is_not_downcastchecks that v3 nanosecond sources keep -1 ns; it fails if the native-type check is removed.Are there any user-facing changes?
With the option on, hour partitions accept timestamp[ns] and identity partitions on timestamp and time columns write every row once. Year, month and day partitions now follow the stored microseconds at negative boundaries (-1 ns becomes 0 us, so partition -1 becomes 0), so filters no longer skip such rows. Existing files retain their old partition values.
Please add the
changeloglabel.This pull request and its description were written by Isaac.